/*
 * Powered By [信安]
 * Since 2013 - 2013
 */


package com.xinan.system.curd;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.core.Const;
import com.jfinal.ext.interceptor.SearchSql;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.xinan.utils.Constants;
import com.xinan.utils.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author EricLee
 * @version 1.0
 * @since 1.0
 */

public class newPasswordController extends CurdController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void index() {
        String tbName = "auth_user";
        String fields = "";
        if (buildView(tbName, fields)) {
            render("/WEB-INF/views/system/index.html");
        }
    }

    /**
     * 在增加了SearchSql拦截之后，就可以根据页面的配置信息
     * 自动装配查询语句，并放入request中，可以通过getAttr(Const.SEARCH_SQL)函数获取到
     * 根据实际需要自动组装到sql语句中
     */
    @Override
    @Before(SearchSql.class)
    public void query() {
        int pageNumber = getParaToInt("page");
        int pageSize = getParaToInt("rows");

        String sqlExp = "from auth_user";
        String where = getAttr(Const.SEARCH_SQL);
        String userName = getAttr(Constants.USER_NAME);
            where += " username = '" + userName + "'";
            sqlExp += " where " + where;
        Page<Record> page = Db.paginate(pageNumber, pageSize, "select *", sqlExp);
        Map root = new HashMap();
     		root.put("total", page.getTotalRow());
     		root.put("rows", page.getList());
     		renderJson(JsonKit.toJson(root));
    }

    /**
     * 修改数据
     */
    @Override
    public void update() {
        boolean flag =true;
        int id=getParaToInt("id");
        String pass=getPara("password");
        String key =Constants.KEY;
        String newpass="";
        newpass = DigestUtils.encrypt(pass, key);
        flag=AuthUser.dao.findById(id).set("password", newpass).update();
        if (flag) {
        	renderText("修改成功");
        } else {
        	renderText("修改失败");
        }
    }

}
